package com.operamasksui;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class UserServicesImpl implements UserServices{

	public List<User> findAllUser() {
		return userInfos;
	}

	public User findUserById(Integer userId) {
		for(int i=0 ; i<userInfos.size() ; i++){
			User user = userInfos.get(i);
			if(user.getId() == userId){
				return user;
			}
		}
		return null;
	}

	public int saveOrUpdate(User user) {
		if(user.getId() != null){ //修改
			this.delete(user);//删除原来的
			this.insert(user);//插入修改后的
		}else{
			user.setId(++userId);
			this.insert(user);
		}
		return 1;
	}

	public int delete(User user) {
		for(int i=0 ; i<userInfos.size() ; i++){
			User userd = userInfos.get(i);
			if(userd.getId() == user.getId()){
			    userInfos.remove(userd);
			}
		}
		return 1;
	}
	
	public int insert(User user)
	{
		userInfos.add(0,user);
		return 1;
	}
	private static int userId = 15;
	public static List<User> userInfos = new ArrayList<User>(
			Arrays.asList(new User[]{
					new User(1,"帆帆", 30, "深圳", "13900000000", "fanfan@apusic.com"),
					new User(2,"发发", 30, "湖北", "13800000000", "fafa@apusic.com"),
					new User(3,"天天", 30, "广州", "13700000000", "tt@apusic.com"),
					new User(4,"真真", 30, "广西", "13600000000", "zz@apusic.com"),
					new User(5,"晓晓", 30, "佛山", "13500000000", "xx@apusic.com"),
					new User(6,"番蕃", 30, "中山", "13400000000", "ff@apusic.com"),
					new User(7,"文文", 30, "四川", "13300000000", "ww@apusic.com"),
					new User(8,"聪聪", 30, "深圳", "13200000000", "cc@apusic.com"),
					new User(9,"刚刚", 30, "湖南", "13100000000", "gg@apusic.com"),
					new User(10,"张三", 30, "深圳", "13910000000", "zs@apusic.com"),
					new User(11,"李四", 30, "深圳", "13920000000", "ls@apusic.com"),
					new User(12,"王五", 30, "深圳", "13930000000", "ww@apusic.com"),
					new User(13,"神雕侠侣", 20, "深圳", "13960000000", "sdxl@apusic.com"),
					new User(14,"帆帆1", 30, "深圳", "13900000000", "fanfan@apusic.com"),
					new User(15,"帆帆2", 30, "深圳", "13900000000", "fanfan@apusic.com")
			})
		);

}
